<?php

namespace Biz\service\wiki;

use Biz\model\wiki\Wiki as mWiki;
use Biz\model\wiki\WikiBasic as mWikiBasic;
use Biz\model\wiki\WikiContent as mWikiContent;

class WikiOtherChannel
{
    private $channel = null;
    private $channelType = null;

    public function __construct($channel, $channelType)
    {
        $this->channel = $channel;
        $this->channelType = $channelType;
    }

    public function create($title, $jump_params, $brand, $spu, $cname, $cover_img, $min_price, $max_price, $content, $images)
    {
        $data = [
            'title'                 => $title,
            'channel'               => $this->channel,
            'channel_type'          => $this->channelType,
            'channel_jump_params'   => $jump_params,
            'channel_brand'         => $brand,
            'channel_shop_id'       => 0,
            'channel_shop_name'     => '',
            'channel_spu_id'        => $spu,
            'channel_category_name' => $cname,
            'category_id'           => 0,
            'second_category_id'    => 0,
            'last_category_id'      => 0,
            'cover_img'             => $cover_img,
            'min_price'             => $min_price,
            'max_price'             => $max_price,
            'status'                => mWiki::STATUS_WAIT_CHECK,
        ];

        $contentData = [
            'imgs'    => $images ? json_encode($images, true) : '',
            'content' => $content,
        ];
        $wikiInfo = mWiki::getInfoByChannelSpu($this->channel, $spu);
        if ($wikiInfo) {
            $id = $wikiInfo['id'] ?? 0;
            mWiki::updateData($data, ['id' => $id]);
            $contentInfo = mWikiContent::getByWhere(['wiki_id' => $id]);
            if ($contentInfo) {
                if ($wikiInfo['status'] != mWiki::STATUS_CHECKED) {
                    if ($contentInfo['imgs'] == $contentData['imgs']) unset($contentData['imgs']);
                    if ($contentInfo['content'] == $contentData['content']) unset($contentData['content']);
                    if ($contentData) mWikiContent::updateData($contentData, ['wiki_id' => $id]);
                }
            } else {
                $contentData['wiki_id'] = $id;
                mWikiContent::insertData($contentData);
            }
            return ;
        }

        $wikiBasicData = [
            'brand_id'           => 0,
            'title'              => $title,
            'basic_state'        => mWikiBasic::STATE_NORMAL,
            'category_id'        => $data['category_id'],
            'second_category_id' => $data['second_category_id'],
            'last_category_id'   => $data['last_category_id'],
        ];
        $basicId = mWikiBasic::insertData($wikiBasicData);
        $data['basic_id'] = $basicId;
        $data['source_basic_id'] = $basicId;
        $data['is_default'] = 1;
        $wikiId = mWiki::insertData($data);
        $contentData['wiki_id'] = $wikiId;
        mWikiContent::insertData($contentData);
    }

}